package com.controller.secController;

import com.service.SecKillService;
import com.controller.Thread.CustomThreadPool;
import com.dao.model.Product;
import com.msg.AppResponseMsg;
import com.msg.ReturnMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.redis.JedisTool ;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/kill")
public class SecKillController {

    private static Logger logger = LoggerFactory.getLogger(SecKillController.class);

    @Autowired
    private SecKillService service;

    /**
     * 设置秒杀商品
     * @param product
     * @return
     */
    @RequestMapping("/setSecProduct")
    @ResponseBody
    public ReturnMsg setSecProduct(Product product){
         service.setSecProduct(product);
        JedisTool.getJedis().set(product.getId()+"",product.getNumber()+"");
        return AppResponseMsg.success("添加秒杀商品成功");
    }

    /**
     * 编辑秒杀商品
     * @param product
     * @return
     */
    @RequestMapping("/delSecProduct")
    @ResponseBody
    public ReturnMsg delSecProduct(Product product){
        service.editSecProduct(product);
        return AppResponseMsg.success("编辑秒杀商品成功");
    }

    /**
     * 查询秒杀商品
     * @param product
     * @return
     */
    @RequestMapping("/querySecProduct")
    @ResponseBody
    public ReturnMsg querySecProduct(Product product){
       List<Product> list = service.querySecProduct(product);
       String total = service.querySecProductTotal(product);
        return AppResponseMsg.success(list,total);
    }

    /**
     * 获得秒杀入口
     * @return
     */
    @RequestMapping("/getCurrentTime")
    @ResponseBody
    public ReturnMsg getCurrentTime(String id) throws ParseException {
        Product product =   service.queryProductById(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long startTime = dateFormat.parse(product.getStart()).getTime();
        Long endTime = dateFormat.parse(product.getEnd()).getTime();

        Long currentTime = new Date().getTime();
        if (startTime - currentTime >0){
         return  AppResponseMsg.faild(startTime.toString());
        }
        if(endTime - currentTime <0){
            return  AppResponseMsg.faild("已经抢购完");
        }
        String spnum = JedisTool.getJedis().get(product.getId());
        if(!StringUtils.isEmpty(spnum)) {
            int num = Integer.parseInt(spnum.trim());
            if (num <= 0) {
                return AppResponseMsg.faild("已经抢购完");
            }
        }else{
            return AppResponseMsg.faild("false");
        }
        return  AppResponseMsg.success(endTime.toString());
    }

    /**
     * 秒杀商品过程
     * @param product
     * @param account
     * @return
     * 默认进入接口就请购数量
     */
    @RequestMapping("/getkill")
    @ResponseBody
    public ReturnMsg getKill(Product product,String account){
        String s = JedisTool.getJedis().get(product.getId());
        if(!StringUtils.isEmpty(s)) {
            int num = Integer.parseInt(s.trim());
            if (num <= 0) {
                return AppResponseMsg.faild("秒杀商品已经售空");
            }
        }else {
            return AppResponseMsg.faild("该商品还没有设置秒杀商品");
        }
        //有请求进来默认减1
        JedisTool.getJedis().decr(product.getId());
        //进行一系列的操作
        try {
            CartController cart =new CartController();
            cart.setAccount(account);
            cart.setOrderbh(new Date().getTime()+"");
            cart.setProduct(product);
            //生成订单,省去支付过程,支付成功,减去redis的数量
            CustomThreadPool.execute(cart);
            //模拟支付过程
            Thread.sleep(2000);

        } catch (InterruptedException e) {
            //操作失败,默认加1
            JedisTool.getJedis().incr(product.getName());
            e.printStackTrace();
        }
        return AppResponseMsg.success("抢购成功");
    }
}
